package{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.events.Event;
	import ro.fwd.DataModel;
	import ro.fwd.ImageScreen;
	import ro.fwd.Controls;
	import ro.fwd.utils.FullScreenMenu;
	
	public class Main extends Sprite{
		/*
		THIS CLASS IS THE MAIN CLASS IS INSTANTIATING  THE GALLERY CLASSES AND ADDING THE FUNCTIONALITY
		*/
		private var _dataModel:DataModel;//holds a reference to the DataModel instance
		private var _imageScreen:ImageScreen;//holds a reference to the ImageScreen instance
		private var _controls:Controls;//holds a reference to the Controls instance
		private var _contextMenu:FullScreenMenu//holds a reference to FullScreenMenu
		
		//constructor
		public function Main(){
			//preparing the stage 
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			//instantiate the DataModel and adding an update INIT even UPDATE  event
			_dataModel =  new DataModel("load/images.xml");
			_dataModel.addEventListener(DataModel.INIT, initGallery);
			_dataModel.addEventListener(DataModel.UPDATE, updateGallery);
		}
		
		//this function instantiates all the clasees needed 
		private function initGallery(e:Event):void{
			_contextMenu = new FullScreenMenu(this);
			_imageScreen =  new ImageScreen();
			addChild(_imageScreen);
			_controls = new Controls(_dataModel,_imageScreen);
			addChild(_controls);
			_controls.animate();
			//after the controls animates we can create the first image
			_controls.addEventListener(Controls.ANIMATION_DONE,createFristImage);
		}
		
		//creates and load the first image
		private function createFristImage(e:Event):void{
			_imageScreen.createImage(_dataModel.getImage());
		}
		
		//creates and load an image everytime the DataModel changes
		private function updateGallery(e:Event):void{
			_imageScreen.createImage(_dataModel.getImage());
		}
	}
}